package com.example.xinli.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.xinli.dao.*;
import com.example.xinli.domain.entity.*;
import com.example.xinli.domain.vo.*;
import com.example.xinli.service.IInterService;
import com.example.xinli.service.IMoodDiaryService;
import com.example.xinli.utils.TimestampUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class IMoodDiaryServiceImpl extends ServiceImpl<MoodDiaryMapper, MoodDiary> implements IMoodDiaryService {

    @Autowired
    private MoodDiaryMapper moodDiaryMapper;

    @Override
    public ResponseBean getMoodDiaryByUserId(String userId) {
        QueryWrapper<MoodDiary> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        List<MoodDiary> diaryList = moodDiaryMapper.selectList(queryWrapper);
        List<MoodDiaryReturnVo> returnVoList = new ArrayList<>();
        for(MoodDiary moodDiary:diaryList) {
            MoodDiaryReturnVo moodDiaryReturnVo = new MoodDiaryReturnVo(
                    moodDiary.getDiaryId(),
                    moodDiary.getUserId(),
                    moodDiary.getDiaryContent(),
                    moodDiary.getDiaryDate(),
                    moodDiary.getDiaryMood(),
                    TimestampUtil.getDay(moodDiary.getDiaryDate()),
                    TimestampUtil.getTime(moodDiary.getDiaryDate())
            );
            returnVoList.add(moodDiaryReturnVo);
        }
        return ResponseBean.success(returnVoList);
    }

    @Override
    public ResponseBean addMoodDiary(MoodDiaryVo moodDiaryVo) {
        MoodDiary moodDiary = new MoodDiary(
                moodDiaryVo.getUserId(),
                moodDiaryVo.getDiaryContent(),
                moodDiaryVo.getDiaryDate(),
                moodDiaryVo.getDiaryMood());
        int res = moodDiaryMapper.insert(moodDiary);
        if (res > 0) {
            return ResponseBean.success();
        } else {
            return ResponseBean.error(ResponseBeanEnum.ERROR);
        }
    }

    @Override
    public ResponseBean removeMoodDiary(int diaryId) {
        QueryWrapper<MoodDiary> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("diary_id", diaryId);
        int res = moodDiaryMapper.delete(queryWrapper);
        if (res > 0) {
            return ResponseBean.success();
        } else {
            return ResponseBean.error(ResponseBeanEnum.ERROR);
        }
    }

    @Override
    public ResponseBean editMoodDiary(MoodDiaryVo moodDiaryVo) {
        QueryWrapper<MoodDiary> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", moodDiaryVo.getDiary_id());
        queryWrapper.eq("diary_id", moodDiaryVo.getDiary_id());
        MoodDiary moodDiary = moodDiaryMapper.selectOne(queryWrapper);
        if(moodDiary == null) {
            return ResponseBean.error(ResponseBeanEnum.ERROR);
        }
        moodDiary.setDiaryContent(moodDiaryVo.getDiaryContent());
        moodDiary.setDiaryMood(moodDiaryVo.getDiaryMood());

        int res = moodDiaryMapper.update(moodDiary, queryWrapper);
        if (res > 0) {
            return ResponseBean.success();
        } else {
            return ResponseBean.error(ResponseBeanEnum.ERROR);
        }
    }
}

